package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.dao.MemberDao;
import com.entity.Result;
import com.pojo.Member;
import com.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*TODO 待检查项*/
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private MemberDao memberDao;

    //吧手机号传递业务层开始更具体操作
    public Result loginCheck(String telephone) {
        //分析流程:去查询用户表看看是否有该用户,有吧用户存入Redis,登录成功,
        // 没有给他注册下,再存入Redis,并把手机会以cookie回存,登录成功
        //注意事项:暂时没有
        Member mem = memberDao.findMemberByTelephone(telephone);
        if (mem == null) {
            //说明还没有注册过此时给他注册,
            Member member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberDao.addMember(member);
            //存入缓存,保存字符串类型的json串
            String json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone, 300 * 6, json);
        } else {
            //说明有会员把他存入缓存
            String json = JSON.toJSON(mem).toString();
            jedisPool.getResource().setex(telephone, 300 * 6, json);
        }
        return new Result(true, "登录成功");
    }

    //查询用户相关数据
    public List<Integer> getMemberData(List<String> list) {
        /*分析:我要根据当前传递过来的日期去查询
        1前台需要之前12个月的信息,以及相关的数据
        * */
        List<Integer> aa=null;
        if (list!=null){
             aa = new ArrayList<>();
            for (String s : list) {
                String date = s + ".31";
               Integer i= memberDao.findMemberCountBeforeDate(date);
            aa.add(i);
            }
        }
        return aa;
    }
}
